package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.impl.ProductclassifiCationTableDaoImpl;
import com.ysd.dao.impl.ProductdetailsinfoDaoImpl;
import com.ysd.dao.impl.xinwenDaoImpl;
import com.ysd.entity.ProductclassifiCationTable;
import com.ysd.entity.Productdetailsinfo;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminXiangQingServlet extends BaseServlet {
	
	public void getAdminProductByWhere(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String productProductName = request.getParameter("productProductName");//产品名称
		int cId = 0;//产品所属分类
		if(request.getParameter("cId")!=null && !request.getParameter("cId").equals("")) {
			cId = Integer.parseInt(request.getParameter("cId"));
		}
		String img = request.getParameter("img");//产品图片
		String productMaterial = request.getParameter("productMaterial");//产品材质
		int productid = 0;//产品初始价格
		if(request.getParameter("productid")!=null && !request.getParameter("productid").equals("")) {
			productid = Integer.parseInt(request.getParameter("productid"));
		}
		int beginproductPrice = 0;//产品初始价格
		if(request.getParameter("beginproductPrice")!=null && !request.getParameter("beginproductPrice").equals("")) {
			beginproductPrice = Integer.parseInt(request.getParameter("beginproductPrice"));
		}
		int endproductPrice = 0;//产品最终价格
		if(request.getParameter("endproductPrice")!=null && !request.getParameter("endproductPrice").equals("")) {
			endproductPrice = Integer.parseInt(request.getParameter("endproductPrice"));
		}
		int productPrice = 0;//产品先价格
		if(request.getParameter("productPrice")!=null && !request.getParameter("productPrice").equals("")) {
			productPrice = Integer.parseInt(request.getParameter("productPrice"));
		}
		int marketPrice = 0;//产品原价格
		if(request.getParameter("marketPrice")!=null && !request.getParameter("marketPrice").equals("")) {
			marketPrice = Integer.parseInt(request.getParameter("marketPrice"));
		}
		String productBrand = request.getParameter("productBrand");//产品品牌
		String color = request.getParameter("color");//产品颜色
		String style = request.getParameter("style");//产品风格
		//获取前台传来的参数
		//将前台传来的参数放到p里
		
		Productdetailsinfo p = new Productdetailsinfo(productProductName, cId, img, productid, productMaterial, beginproductPrice, endproductPrice, productPrice, marketPrice, productBrand, color, style, "");
		ProductdetailsinfoDaoImpl pDao = new ProductdetailsinfoDaoImpl();
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<Productdetailsinfo> dList = pDao.getProductdetailsinfoByPage(p, page, size);//调用实现类中的方法	
		response.setCharacterEncoding("UTF-8");
		
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		String res = gson.toJson(dList);
		out.print(res);
		
		out.flush();
		out.close();
	}
	
	public void getAllProductByWhere(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String productProductName = request.getParameter("productProductName");//产品名称
		int cId = 0;//产品所属分类
		if(request.getParameter("cId")!=null && !request.getParameter("cId").equals("")) {
			cId = Integer.parseInt(request.getParameter("cId"));
		}
		String img = request.getParameter("img");//产品图片
		String productMaterial = request.getParameter("productMaterial");//产品材质
		int productid = 0;//产品初始价格
		if(request.getParameter("productid")!=null && !request.getParameter("productid").equals("")) {
			productid = Integer.parseInt(request.getParameter("productid"));
		}
		int beginproductPrice = 0;//产品初始价格
		if(request.getParameter("beginproductPrice")!=null && !request.getParameter("beginproductPrice").equals("")) {
			beginproductPrice = Integer.parseInt(request.getParameter("beginproductPrice"));
		}
		int endproductPrice = 0;//产品最终价格
		if(request.getParameter("endproductPrice")!=null && !request.getParameter("endproductPrice").equals("")) {
			endproductPrice = Integer.parseInt(request.getParameter("endproductPrice"));
		}
		int productPrice = 0;//产品先价格
		if(request.getParameter("productPrice")!=null && !request.getParameter("productPrice").equals("")) {
			productPrice = Integer.parseInt(request.getParameter("productPrice"));
		}
		int marketPrice = 0;//产品原价格
		if(request.getParameter("marketPrice")!=null && !request.getParameter("marketPrice").equals("")) {
			marketPrice = Integer.parseInt(request.getParameter("marketPrice"));
		}
		String productBrand = request.getParameter("productBrand");//产品品牌
		String color = request.getParameter("color");//产品颜色
		String style = request.getParameter("style");//产品风格
		//获取前台传来的参数
		//将前台传来的参数放到p里
		Productdetailsinfo p = new Productdetailsinfo(productProductName, cId, img, productid, productMaterial, beginproductPrice, endproductPrice, productPrice, marketPrice, productBrand, color, style, "");
		ProductdetailsinfoDaoImpl pDao = new ProductdetailsinfoDaoImpl();
		List<Productdetailsinfo> dList = pDao.getProductdetailsinfo(p);//调用实现类中的方法	
		response.setCharacterEncoding("UTF-8");
		
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		String res = gson.toJson(dList.size());
		out.print(res);
		
		out.flush();
		out.close();
	}
	
	public void delxiangqing(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int cId = Integer.parseInt(request.getParameter("productid"));
		ProductdetailsinfoDaoImpl pcDao = new ProductdetailsinfoDaoImpl();
		int n= pcDao.delcanpin(cId);
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		if (n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		} else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		out.flush();
		out.close();
	}
	public void addxiangqing(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		ProductdetailsinfoDaoImpl pcDao = new ProductdetailsinfoDaoImpl();

		String productProductName = request.getParameter("productProductName");//产品名称
		int cId = 0;//产品所属分类
		if(request.getParameter("cId")!=null && !request.getParameter("cId").equals("")) {
			cId = Integer.parseInt(request.getParameter("cId"));
		}
		String img = request.getParameter("img");//产品图片
		String productMaterial = request.getParameter("productMaterial");//产品材质
		
		int productPrice = 0;//产品先价格
		if(request.getParameter("productPrice")!=null && !request.getParameter("productPrice").equals("")) {
			productPrice = Integer.parseInt(request.getParameter("productPrice"));
		}
		int marketPrice = 0;//产品原价格
		if(request.getParameter("marketPrice")!=null && !request.getParameter("marketPrice").equals("")) {
			marketPrice = Integer.parseInt(request.getParameter("marketPrice"));
		}
		String productBrand = request.getParameter("productBrand");//产品品牌
		String color = request.getParameter("color");//产品颜色
		String style = request.getParameter("style");//产品风格
		
		//接受前台传来的参数
		if (productProductName!=null&&!productProductName.equals("")) {
			List<Productdetailsinfo> pcList = pcDao.getPinfo(productProductName);
			//创建实现类对象
			if (pcList.size()==0) {
				Productdetailsinfo pc = new Productdetailsinfo(productProductName, cId, img, 0, productMaterial, productPrice, marketPrice, productBrand, color, style);
				//等于零的时候可以添加
				//调用构造方法，创建餐品详情的对象
				System.out.println(pc);
				//打印
				
				int n= pcDao.addinfo(pc);
				// 调用实现类的构造方法执行sql语句返回受影响行数
				if (n>0) {
					out.print(Result.toClient(true, "成功",""));
					//大于0成功
				} else {
					out.print(Result.toClient(false, "删除失败",""));
					//否则失败
				}
			} else {
				out.print(Result.toClient(false, "分类名已存在",""));
			}
		}
		
		out.flush();
		//清空
		out.close();
		//关闭
	}
	//
	public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		
		//1创建SmartUpload对象
		SmartUpload smart = new SmartUpload();
		
		//2设置编码格式
		smart.setCharset("UTF-8");
		
		//3初始化
		smart.initialize(getServletConfig(),request,response);
		
		try {
			//4上传
			smart.upload();
			//5设置上传文件名字
			String fileName = System.currentTimeMillis() + ".jpg";
			//将文件保存到制定目录
			smart.getFiles().getFile(0).saveAs("/images/" + fileName);
			//响应
			out.print(Result.toClient(true, "上传成功",fileName));
		} catch (SmartUploadException e) {
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败","图片格式不符"));
		}
		out.flush();
		out.close();
	}
	public void geproductinfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		ProductdetailsinfoDaoImpl pcDao = new ProductdetailsinfoDaoImpl();
		int cateId = Integer.parseInt(request.getParameter("cId"));
		Productdetailsinfo pc = pcDao.getproductbyid(cateId);
		Gson gson = new Gson();
		String res = gson.toJson(pc);
		out.print(res);
		
		out.flush();
		//清空
		out.close();
		//关闭
	}
	public void updateproductid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ProductdetailsinfoDaoImpl pcDao = new ProductdetailsinfoDaoImpl();
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		int productid = Integer.parseInt(request.getParameter("productid"));
		int productPrice = Integer.parseInt(request.getParameter("productPrice"));
		int marketPrice = Integer.parseInt(request.getParameter("marketPrice"));
		int cId = Integer.parseInt(request.getParameter("cId"));
		String productMaterial = request.getParameter("productMaterial");
		String productProductName = request.getParameter("productProductName");
		String productBrand = request.getParameter("productBrand");
		String img = request.getParameter("img");
		String color = request.getParameter("color");
		String style = request.getParameter("style");
		Productdetailsinfo pc = new Productdetailsinfo(productProductName, cId, img, productid, productMaterial, productPrice, marketPrice, productBrand, color, style);
		int n = pcDao.updateProductdetailsinfo(pc);
		if (n>0) {
			out.print(Result.toClient(true, "修改成功",""));
		} else {
			out.print(Result.toClient(false, "修改失败",""));
		}
		out.flush();
		out.close();
	}

}
